package cn.sccl.dirms.security.controller;

import cn.sccl.common.model.PageResult;
import cn.sccl.common.model.QueryData;
import cn.sccl.common.util.SecurityUtils;
import cn.sccl.dirms.security.model.UserPrincipal;
import com.fasterxml.jackson.annotation.JsonView;
import cn.sccl.common.web.BaseController;
import cn.sccl.dirms.security.model.Authority;
import cn.sccl.dirms.security.model.MenuAction;
import cn.sccl.dirms.security.service.AuthorityManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 杨浩宇 on 2016-02-24.
 */
@RestController
@RequestMapping(value = "/dirms/security/authority")
public class AuthorityController extends BaseController {

    public static final String HOTSPOT_RP = "hotspot_rp"; //热点报表
    public static final String HOTSPOT_LINK = "hotspot_link";//热点快捷方式
    @Autowired
    private AuthorityManager authorityManager;

    /**
     * 显示所有数据
     */
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<Authority> listAllAuthority() {
        return authorityManager.listAll();
    }

    /**
     * 通过用户ID查找功能菜单权限
     */

    @RequestMapping(value = "/find", method = RequestMethod.GET)
    public List<Authority> findbyuser(HttpServletRequest request) {
        UserPrincipal  u =  SecurityUtils.getUserPrincipal(request.getSession());
        return authorityManager.findByUser((u==null?"":u.getId()),(u==null?"":u.getName()));
    }

    @RequestMapping(value = "/findUniqueBy", method = {RequestMethod.GET,RequestMethod.POST})
    public Authority findUniqueBy(HttpServletRequest request) {
        QueryData queryData = getQueryData(request);
        if (queryData !=null && queryData.size() > 0){
            return authorityManager.findUniqueBy(queryData);
        }
        return null;
    }
    /**
     * 通过用户ID查找功能菜单权限
     */

    @RequestMapping(value = "/find/all", method = RequestMethod.GET)
    public List<Authority> findAll() {
        return authorityManager.findAll();
    }
    /**
     * 分页查询
     *
     * @param pageIndex 页码
     * @param pageSize  分页大小
     * @param request
     * @return
     */
    @RequestMapping(value = "/page/{pageIndex}/{pageSize}", method = RequestMethod.GET)
    public PageResult<Authority> pageQueryAuthority(@PathVariable("pageIndex") long pageIndex, @PathVariable("pageSize") int pageSize, HttpServletRequest request) {
        return authorityManager.pageQueryByLike(pageIndex, pageSize, getQueryData(request));
    }

    /**
     * 保存数据
     *
     * @param authority
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public Authority saveAuthority(@RequestBody Authority authority) {
        authorityManager.save(authority);
        return authority;
    }

    /**
     * 根据ID删除数据
     *
     * @param id
     */
//    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @RequestMapping(value = "/{id}", method = {RequestMethod.POST,RequestMethod.GET})
    @JsonView(Object.class)
    public boolean deleteAuthority(@PathVariable("id") String id) {
        authorityManager.delete(id);
        return true;
    }

    /**
     * 根据用户ID查询具有的导航菜单
     *
     * @return
     */
    @RequestMapping(value = "/findNavAuthoritesByUser", method = RequestMethod.GET)
    public List<Authority> findNavAuthoritesByUser(HttpServletRequest request) {
        UserPrincipal u = SecurityUtils.getUserPrincipal(request.getSession());
        return authorityManager.findNavAuthoritesByUser(u==null?"":u.getId());
    }


    /**
     * 根据场景查询菜单
     * t_sec_authorities.sence字段
     *
     * @return
     */
    @RequestMapping(value = "/findBySence/{sence}", method = RequestMethod.GET)
    public List<Authority> findBySence(@PathVariable("sence") String sence,HttpServletRequest request) {

        UserPrincipal u = SecurityUtils.getUserPrincipal(request.getSession());
        return authorityManager.findBySence(u==null?"":u.getId(),sence);
    }

    /**
     * 根据场景查询菜单
     * t_sec_authorities.sence字段
     *
     * @return
     */
    @RequestMapping(value = "/single/findBySence/{sence}", method = RequestMethod.GET)
    public Authority findAuthorityBySence(@PathVariable("sence") String sence) {
        Map<String,Object> params = new HashMap<>();
        params.put("sence",sence);
        return authorityManager.findUniqueBy(params);
    }

    @RequestMapping(value = "/findChildren", method = RequestMethod.GET)
    public List<MenuAction> findChildren(HttpServletRequest request) {
        String id = request.getParameter("id");
        return authorityManager.findChildren(id);
    }
}
